<?php
class FileExtractor implements Extractor {
	private static $ERROR_MESSAGE = "Error : can't open file";
	private static $DATA_DIR = "DB/";

	function __construct() {
		if (!file_exists(FileExtractor::$DATA_DIR)) {
			mkdir(FileExtractor::$DATA_DIR);
			chdir(FileExtractor::$DATA_DIR);
		}
	}

	private function create(Persistance $persistance) {
		$reflection = new ReflectionClass($persistance);
		if (!file_exists($reflection->getName())) mkdir($reflection->getName());
		chdir($reflection->getName());
		$file = fopen($persistance->getId().".dat", "w") or die(FileExtractor::$ERROR_MESSAGE);
		return $file;
	}

	public function insert(Persistance $persistance) {
		$file = $this->create($persistance);

		echo serialize($persistance);
		fwrite($file, serialize($persistance)."\n");

		fclose($file);
	}

	public function delete(Persistance $persistance) {
		unlink($persistance->getId().".dat");
	}

	public function query(Persistance $persistance, Criteria $criteria) {
		
	}
}
?>